USE [msdb]
GO

/****** Object:  Job [Back Up GateLog month]    Script Date: 05/16/2012 17:16:05 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [[Uncategorized (Local)]]]    Script Date: 05/16/2012 17:16:05 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

END

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'Back Up GateLog month', 
		@enabled=1, 
		@notify_level_eventlog=0, 
		@notify_level_email=0, 
		@notify_level_netsend=0, 
		@notify_level_page=0, 
		@delete_level=0, 
		@description=N'бекапирует историю логов и удаляет из logW за 200 дней все', 
		@category_name=N'[Uncategorized (Local)]', 
		@owner_login_name=N'ALTER-I\knovikov', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [backup and delete]    Script Date: 05/16/2012 17:16:07 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'backup and delete', 
		@step_id=1, 
		@cmdexec_success_code=0, 
		@on_success_action=3, 
		@on_success_step_id=0, 
		@on_fail_action=3, 
		@on_fail_step_id=0, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'INSERT INTO [Gate_LOG].[dbo].[AutomatLogW]
     ( ID,  [sms_id],   [DateTime], [DTA],    [Automat], [Sec], [Phone],
      [Sum],[Operation],[PA],       [Margin], [Result],  [Res], [Ext],   [BS])
   SELECT  al.ID, al.[sms_id]  ,al.[DateTime]   ,al.[DTA]  ,al.[Automat],al.[Sec],al.[Phone],
      al.[Sum],al.[Operation],al.[PA],al.[Margin],al.[Result],al.[Res],al.[Ext],al.[BS]
   FROM [i_dealer].[dbo].[AutomatLogW] al
    left join [Gate_LOG].[dbo].[AutomatLogW] gal on  gal.id = al.id
   where al.[datetime] < cast((GetDate()-0.5) as int) - 100
     and gal.id is null
   
IF(@@Error = 0) BEGIN
    DELETE FROM i_dealer.dbo.automatlogW
	WHERE(    phone is  null  or phone = ''''   
           or phone like ''000998%''  or phone  like ''000999%''  or phone = ''7'' 
           or operation = ''ERR#602'' or operation = ''ERR#601'')
      and operation not in  (''ERR#1126'',''INCASSO'',''NO_INCASSO'',''Reg'',''INC'',''PINOK'',''INCOK'',''PINALERT'',''MANUAL_INC'',''ERR#605'')
      and [datetime] < cast((GetDate()-0.5) as int) - 100




   DECLARE  @Nam varchar(20),  @disc varchar(50); 
   SET @Nam = cast(YEAR(GetDate()) as varchar) + ''_'' + cast(Month(GetDate()) as varchar)
   SET @disc = ''G:\backup\log_'' + @Nam + ''.bak'';
   SET @Nam = ''Full_Gate_Log_'' + @Nam ;


   BACKUP DATABASE [Gate_LOG] TO  DISK    = @disc
   WITH NOFORMAT, INIT,                      NAME = @Nam, 
            SKIP, NOREWIND, NOUNLOAD,  STATS = 10
 
   TRUNCATE TABLE  [Gate_LOG].[dbo].[AutomatLogW]



    --- DELETE FROM  Gate_LOG.dbo.automatlogW
    ---  where [datetime] < cast((GetDate()-0.5) as int) - 200


END', 
		@database_name=N'master', 
		@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [backup full i_deaelr month]    Script Date: 05/16/2012 17:16:08 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'backup full i_deaelr month', 
		@step_id=2, 
		@cmdexec_success_code=0, 
		@on_success_action=1, 
		@on_success_step_id=0, 
		@on_fail_action=2, 
		@on_fail_step_id=0, 
		@retry_attempts=0, 
		@retry_interval=0, 
		@os_run_priority=0, @subsystem=N'TSQL', 
		@command=N'BACKUP DATABASE [i_dealer]		
TO  DISK = ''e:/backup/i_deaer.bak''
WITH  DESCRIPTION = ''full backup i_dealer'',   NOFORMAT, INIT,  
NAME = ''i_dealer'',	SKIP, REWIND, NOUNLOAD,  STATS = 10, no_log', 
		@database_name=N'master', 
		@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'Gate_log bakup', 
		@enabled=1, 
		@freq_type=32, 
		@freq_interval=2, 
		@freq_subday_type=1, 
		@freq_subday_interval=0, 
		@freq_relative_interval=8, 
		@freq_recurrence_factor=1, 
		@active_start_date=20090625, 
		@active_end_date=99991231, 
		@active_start_time=10000, 
		@active_end_time=235959, 
		@schedule_uid=N'c365a1f7-09ce-4e5e-b043-d878a71f4cb0'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:

GO

